package com.wjw.cloud.controller;

import com.wjw.cloud.common.api.CommonPage;
import com.wjw.cloud.common.api.CommonResult;
import com.wjw.cloud.dto.GoodsParam;
import com.wjw.cloud.model.Goods;
import com.wjw.cloud.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "GoodsController", description = "商品管理")
@Controller
@RequestMapping("/goodsList")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation("添加商品数据")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult insert(@Validated @RequestBody GoodsParam goodsParam)
    {
        int count = goodsService.insert(goodsParam);

        if (count < 1)
        {
            return CommonResult.failed();
        }

        return CommonResult.success(count);
    }

    @ApiOperation("删除商品数据")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult insert(@PathVariable("id") Integer id)
    {
        int count = goodsService.delete(id);

        if (count < 1)
        {
            return CommonResult.failed();
        }

        return CommonResult.success(count);
    }

    @ApiOperation("修改商品数据")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult insert(@RequestBody Goods goods)
    {
        int count = goodsService.update(goods);

        if (count < 1)
        {
            return CommonResult.failed();
        }

        return CommonResult.success(count);
    }

    @ApiOperation(value = "根据条件查询商品分页数据")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Goods>> list(@RequestParam(value = "describe", required = false) String describe,
                                                       @RequestParam(value = "storeName", required = false) String storeName,
                                                       @RequestParam(value = "post", required = false) Integer post,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize)
    {
        List<Goods> list = goodsService.list(describe, storeName, post, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "用户商品页面根据条件查询数据")
    @RequestMapping(value = "/frontList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Goods>> frontList(@RequestParam(required = false) Map<String, Object> map)
    {
        List<Goods> list = goodsService.frontList(map);
        return CommonResult.success(CommonPage.restPage(list));
    }

    /**
     * 1. 先从数据库查询数据
     * 2. 把查询出来的数据插入到 es 里面
     * 3. 从 Elastic Search 查询数据
     * @return CommonResult<CommonPage<Goods>>
     */
    @ApiOperation(value = "用户商品页面根据条件查询数据")
    @RequestMapping(value = "/frontListByES", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<HashMap<String, Object>> frontListByES(@RequestParam(required = false) Map<String, Object> map)
    {
        HashMap<String, Object> hashMap = goodsService.frontListByES(map);
        return CommonResult.success(hashMap);
    }

}
